#include "AntiGravityShutter.h"
#include "RSManager.h"
#include "Sprite.h"
#include "Box.h"
#include "ShareComponents.h"
#include "ScubaSoldierBullet.h"
#include "UFOs.h"
#include "MainGame.h"
#include "Grenade.h"

AntiGravityShutter::AntiGravityShutter(Box* box) : Object(box, ObjEnemy_DYNAMID)
{
	_appear = RSManager::getInstance()->getSprite(SHUTTER_APPEAR);
	_disappear = RSManager::getInstance()->getSprite(SHUTTER_DISAPPEAR);
	_waiting = RSManager::getInstance()->getSprite(SHUTTER_WAITING);
	_sprite = _appear;	
	_posY = new float[3];
	_posY[0] = 320;
	_posY[1] = 180;
	_posY[2] = 260;
	delay = 0;
	_delayNextSprite = 0;
	_delayAttack = 0;
	_index = -1;
	_hits = 32;
	_isAlive = true;
	_hide = true;
	_vx = -3;
	_dx = _box->_size.x / 2 - 34;
	_box->_size = _sprite->getRenderSize();


	_posBullet = D3DXVECTOR2(50, 60);
}


AntiGravityShutter::~AntiGravityShutter(void)
{
}

void AntiGravityShutter::update()
{
	if(_hits <= 0)
		_isAlive = false;
	delay += GAME_TIME;
	if (delay < 3000)
	{
		_hide = true;
		setPosition();
	}
	else
	{
		_hide = false;		
		if(_appear->GetCurrentIndex() == 9)
		{
			_sprite = _waiting;
			_appear->Reset(0);
		}
		if(delay > 9000)		
			_sprite = _disappear;
		if(_disappear->GetCurrentIndex() == 9)
		{
			_sprite = _appear;
			_disappear->Reset(0);
			delay = 0;
		}
		

		_delayNextSprite+=GAME_TIME;
		if(_delayNextSprite >= 200)
		{
			_sprite->Next();			
			_delayNextSprite = 0;
		}
		_delayAttack += GAME_TIME;
		if (_delayAttack >= 400)
		{
			_delayAttack = 0;
			if (_sprite == _waiting)
			{				
				attack();
			}			
		}
	}		
}

void AntiGravityShutter::render()
{
	if(!_hide)
		_sprite->Render(_box->_position);	
}

Box* AntiGravityShutter::getCollisionBox()
{
	if(_hide)
	{		
		return gl_BOX_0_0_0;
	}
	_colBox->_position.x = _box->_position.x + 10;
	_colBox->_position.y = _box->_position.y + 10;
	_colBox->_size.x = _box->_size.x - 20;
	_colBox->_size.y = _box->_size.y - 20;
	_colBox->_v = _box->_v;
	return _colBox;
}

void AntiGravityShutter::setPosition()
{
	if(_index == 2)
		_index = -1;
	_box->_position.y = _posY[++_index];	
	_box->_position.x = gl_player->getCenterPos().x - 50;
}

void AntiGravityShutter::attack()
{ 	
	Grenade* gr = new Grenade(new Box(_posBullet + _box->_position, D3DXVECTOR2(0, 0), D3DXVECTOR2(2, -2)), 120, 90);
	gl_MainGame->addWorldObject(gr);

	_vx *= -1;	
	_dx *= -1;
	UFOs* ufo = new UFOs(new Box(_box->_position + _box->_size / 2 + D3DXVECTOR2(_dx, 0), D3DXVECTOR2(0, 0), D3DXVECTOR2(_vx, -3)));	
	gl_MainGame->addWorldObject(ufo);
}
